package algorithm.array;

/**
 * leetcode: https://leetcode-cn.com/problems/teemo-attacking/
 * Difficulty: Medium
 * Created by yzy on 2021-02-26 16:14
 */
public class TemmoAttaking {

    public static void main(String[] args) {
        int[] time = new int[]{89,231};
        int dur = 5;
        int poisonedDuration = findPoisonedDuration(time, dur);
        System.out.println(poisonedDuration);
    }

    /**
     * 2ms, 98.5%
     * @param timeSeries
     * @param duration
     * @return
     */
    public static int findPoisonedDuration(int[] timeSeries, int duration) {
        if(timeSeries.length == 0){
            return 0;
        }

        int posTime = 0;
        int size = timeSeries.length;
        for(int i=0; i<size-1; i++){
            if(timeSeries[i] + duration -1 <timeSeries[i+1]){
                posTime += duration;
            }else{
                posTime += (timeSeries[i+1] - timeSeries[i]);
            }
        }

        return posTime+duration;
    }
}
